import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

///zhudaihao
///2024-1-30
///输入编辑圆角
class EditRounded extends StatefulWidget {
  ///提示语
  final String? hint;

  ///角弧度
  final double? radius;

  ///输入限制
  final List<TextInputFormatter>? inputFormatters;

  ///键盘
  final TextInputType? keyboardType;

  ///编辑输入控制器
  final TextEditingController? controller;

  ///是否显示隐藏密码图标
  final bool passwordVisible;

  ///是否显示输入密码显示隐藏图标
  final Widget? rightWidget;

  const EditRounded(
      {this.hint,
      this.radius,
      this.inputFormatters,
      this.keyboardType,
      this.controller,
      this.passwordVisible = false,
      this.rightWidget,
      Key? key})
      : super(key: key);

  @override
  State<EditRounded> createState() => _EditRoundedState();
}

class _EditRoundedState extends State<EditRounded> {
  //控制密码显示隐藏对应图标
  bool passwordVisible = false;
  var errorTextTag = "";
  late TextEditingController? controller;
  late OutlineInputBorder customBorder;
  bool isShownClean = false;

  // var errorBorder = OutlineInputBorder(
  //   /*边角*/
  //   borderRadius: BorderRadius.all(
  //     Radius.circular(5.w), //边角为5
  //   ),
  //   borderSide: const BorderSide(
  //     color: Colors.red, //边线颜色为白色
  //     width: 1, //边线宽度为2
  //   ),
  // );

  @override
  void initState() {
    passwordVisible = widget.passwordVisible;
    controller = widget.controller ?? TextEditingController();

    customBorder = OutlineInputBorder(
        borderRadius: BorderRadius.circular(widget.radius ?? 10),
        borderSide: BorderSide.none);

    controller?.addListener(() {
      int length = controller?.text.length ?? 0;
      if (length == 0 && isShownClean) {
        setState(() {
          isShownClean = false;
        });
      }
      if (length > 0 && !isShownClean) {
        setState(() {
          isShownClean = true;
        });
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      style: const TextStyle(color: Colors.white, fontSize: 14),
      obscureText: passwordVisible,
      inputFormatters: widget.inputFormatters,
      keyboardType: widget.keyboardType,
      maxLines: 1,
      controller: controller,
      decoration: InputDecoration(
        border: customBorder,
        enabledBorder: customBorder,
        focusedBorder: customBorder,
        focusedErrorBorder: customBorder,
        errorBorder: customBorder,
        // errorText: widget.errorText,
        filled: true,
        fillColor: const Color(0xff323440),
        hintText: widget.hint ?? '',
        hintStyle: const TextStyle(color: Color(0xff6C6F78), fontSize: 14),
        contentPadding: const EdgeInsets.only(left: 16, right: 16),
        suffixIcon: IntrinsicHeight(
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Visibility(
                visible: (widget.controller?.text.length ?? 0) > 0,
                child: IconButton(
                  icon: const Icon(
                    Icons.cancel_sharp,
                    color: Colors.white,
                    size: 16,
                  ),
                  onPressed: () {
                    setState(() {
                      controller?.clear();
                    });
                  },
                  padding: EdgeInsets.zero,
                ),
              ),

              //显示隐藏密码
              Visibility(
                visible: widget.passwordVisible,
                child: InkWell(
                  child: Padding(
                    padding: const EdgeInsets.only(right: 5),
                    child: Icon(
                      passwordVisible ? Icons.visibility_off : Icons.visibility,
                      size: 16,
                    ),
                  ),
                  onTap: () {
                    setState(() {
                      passwordVisible = !passwordVisible;
                    });
                  },
                ),
              ),

              widget.rightWidget ??
                  const SizedBox(
                    width: 0,
                    height: 0,
                  ),
            ],
          ),
        ),
      ),
      textAlign: TextAlign.start,
    );
  }
}
